/*
 * Copyright © 2017 eqxiu.com 北京中网易企秀科技有限公司  All rights reserved.
 */

package cn.hermit.reflection.parameter;

import java.lang.reflect.AccessibleObject;

/**
 * @author JackGao 高建国
 * @description //TODO 设计说明
 * @date 1/6/2017
 */
public class AdaptiveParameterNameFinder implements ParameterNameFinder {

    private final ParameterNameFinder[] finders;

    public AdaptiveParameterNameFinder() {
        this(new AnnotationParameterNameFinder(), new BytecodeParameterNameFinder(), new PositionalParameterNameFinder());
    }

    /**
     * Prioritize a series of ParameterNameFinders
     * @param finders the ParameterNameFinders in question
     */
    public AdaptiveParameterNameFinder(ParameterNameFinder... finders) {
        this.finders = finders;
    }

    public String[] findParameterNames(AccessibleObject methodOrConstructor) {
        return findParameterNames(methodOrConstructor, true);
    }

    public String[] findParameterNames(AccessibleObject methodOrCtor, boolean throwExceptionIfMissing) {
        for (int i = 0; i < finders.length; i++) {
            ParameterNameFinder finder = finders[i];
            String[] names = finder.findParameterNames(methodOrCtor, i+1 < finders.length ? false : throwExceptionIfMissing);
            if (names != EMPTY_NAMES) {
                return names;
            }
        }
        return EMPTY_NAMES;
    }
}
